package com.atguigu.system.controller;


import com.atguigu.common.result.Result;
import com.atguigu.system.model.entity.InventoryFlow;
import com.atguigu.system.service.InventoryFlowService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/system/inventory-flow")
public class InventoryFlowController {

    @Autowired
    private InventoryFlowService inventoryFlowService;

    // 新增库存流水记录
    @PostMapping("/save")
    public Result<?> saveInventoryFlow(@RequestBody InventoryFlow inventoryFlow) {
        boolean save = inventoryFlowService.save(inventoryFlow);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail("新增库存流水记录失败");
        }
    }

    // 根据 ID 删除库存流水记录
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteInventoryFlow(@PathVariable Integer id) {
        boolean remove = inventoryFlowService.removeById(id);
        if (remove) {
            return Result.ok();
        } else {
            return Result.fail("删除库存流水记录失败");
        }
    }

    // 根据 ID 更新库存流水记录
    @PutMapping("/update")
    public Result<?> updateInventoryFlow(@RequestBody InventoryFlow inventoryFlow) {
        boolean update = inventoryFlowService.updateById(inventoryFlow);
        if (update) {
            return Result.ok();
        } else {
            return Result.fail("更新库存流水记录失败");
        }
    }

    // 根据 ID 查询库存流水记录
    @GetMapping("/get/{id}")
    public Result<?> getInventoryFlow(@PathVariable Integer id) {
        InventoryFlow inventoryFlow = inventoryFlowService.getById(id);
        if (inventoryFlow != null) {
            return Result.ok(inventoryFlow);
        } else {
            return Result.fail("未找到该库存流水记录");
        }
    }

    // 分页查询库存流水记录
    @GetMapping("/page")
    public Result<?> pageQuery(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(required = false) String operatorName,
                               @RequestParam(required = false) String operationType,
                               @RequestParam(required = false) Integer inventoryId) {
        Page<InventoryFlow> page = new Page<>(pageNum, pageSize);
        Page<InventoryFlow> resultPage = inventoryFlowService.pageQuery(page, operatorName, operationType, inventoryId);
        return Result.ok(resultPage);
    }

}

